package com.lxy.allappinfo;

import java.util.ArrayList;
import java.util.List;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends FragmentActivity {
	
	 private static final String[] CONTENT = new String[] { "非系统应用","系统应用"};
	
	private List<PackageInfo> packageInfo1;
	private List<PackageInfo> packageInfo2;
	
	private MyFragmentAdapter adapter;
	private TabPageIndicator indicator;
	private ViewPager pager;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        indicator=(TabPageIndicator) findViewById(R.id.indicator);
        pager=(ViewPager) findViewById(R.id.pager);
        
        packageInfo1=new ArrayList<PackageInfo>();
        packageInfo2=new ArrayList<PackageInfo>();
        
        List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
        
        for(PackageInfo pin:packages){
        	if(0==(pin.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)){
 	        	packageInfo1.add(pin);
 	        }else{
 	        	packageInfo2.add(pin);
 	        }
        }
      
        adapter=new MyFragmentAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);
        indicator.setViewPager(pager);
        
    }
    
    class MyFragmentAdapter extends FragmentPagerAdapter{

		public MyFragmentAdapter(FragmentManager fm) {
			super(fm);
		}
		
		@Override
		public Fragment getItem(int position) {
			MyFragment f;
			if(position==0){
				f= MyFragment.getFragment(packageInfo1);
			}else{
				f= MyFragment.getFragment(packageInfo2);
			}
           return f;
		}

		@Override
		public CharSequence getPageTitle(int position) {
           return CONTENT[position % CONTENT.length];
		}

		@Override
		public int getCount() {
           return CONTENT.length;
		}
    }
    
    
}
